<?php
namespace Admin\Controller;

class ClassifyController extends AdminController
{
    public function classify(){
        $res = $this->digui(0);
        // dump($res);
        $this->assign('res',$res);
        $this->display();
    }

    public function digui($id,$arr=array(),$lv){
        $lv++;
        $res = M('classify')->where(array('fid'=>$id))->select();
        if($res){
            foreach($res as $v){
                $v['lv'] = $lv;
                $arr[]=$v;
                $arr = $this->digui($v['id'],$arr,$lv);
            }
        }
        return $arr;
    }

    public function ajaxPage(){
        echo '点击添加子元素';
    }

    public function ajaxNull(){
        echo '';
    }

    public function update(){
        $id = I('get.id');
        $res = M('classify')->where(array('id'=>$id))->setField('active','1');
        if($res){
            $this->success('状态更改成功');
        }
    }

    public function delete(){
        $id = I('get.id');
        $res = M('classify')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('已成功删除该分类');
        }
    }

    public function add(){
        if(IS_POST){
            $fid = session('fid');
            $classify = D('classify');
            $_POST['fid'] = $fid;
            $res = $classify->create(I('post.'));
            if($res){
                if($classify->add()){
                    session('fid',null);
                    $this->success('添加成功',U('classify'));
                }else{
                    $this->error($classify->getError());
                }
            }else{
                $this->error($classify->getError());
            }
        }else{
            if(I('get.fid')){
                $id = I('get.fid');
            }else{
                $id = '0';
            }
            session('fid',$id);
            $this->display();
        }
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('classify', $map );
                break;
            case 'resumeuser':
                $this->resume('classify', $map );
                break;
            case 'deleteuser':
                $this->delete('classify', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}
